#include "gtest/gtest.h"
#include <mockcpp/mokc.h>
#include <mockcpp/mockcpp.hpp>

#include <vector>
#include <iostream>

#include "topoinfo_struct.h"
#include "log.h"
#include "checker.h"
#include "virtual_topo.h"
#include "testcase_utils.h"
#include "coll_native_executor_base.h"

using namespace hccl;

std::vector<string> allGatherAlgName = {
    "AllGatherMeshExecutor",
    "AllGatherMeshOpbaseExecutor",
    "AllGatherMeshOpbasePipelineExecutor",
    "AllGatherRingExecutor",
};

class AllGatherTest : public testing::Test {
protected:
    static void SetUpTestCase()
    {
        std::cout << "AllGatherTest set up." << std::endl;
    }

    static void TearDownTestCase()
    {
        std::cout << "AllGatherTest tear down." << std::endl;
    }

    virtual void SetUp()
    {
        const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
        std::string caseName = "analysis_result_" + std::string(test_info->test_case_name()) + "_" + std::string(test_info->name());
        Checker::SetDumpFileName(caseName);
    }

    virtual void TearDown()
    {
        Checker::SetDumpFileName("analysis_result");
        GlobalMockObject::verify();
        // 这边每个case执行完成需要清理所有的环境变量，如果有新增的环境变量，需要在这个函数中进行清理
        ClearHcclEnv();
    }
};

TEST_F(AllGatherTest, allgather_test)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 2);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;

    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);

    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_test_loop)
{
    RankTable_For_LLT gen;
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.reduceType = HcclReduceOp::HCCL_REDUCE_SUM;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;

    HcclResult ret;
    for (int i = 1; i <= 2; i++) {
        for (int j = 1; j <= 8; j++) {
            Checker checker;
            RankTable_t ranktable;
            TopoMeta topoMeta;
            gen.GenTopoMeta(topoMeta, 1, i, j);
            gen.GenRankTable(topoMeta, ranktable);
            ret = checker.Check(testOpParam, ranktable, topoMeta);
            EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
        }
    }
}

TEST_F(AllGatherTest, allgather_offload_AllGatherMeshExecutor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherMeshExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_offload_AllGatherRingExecutor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherRingExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

// 仅测试用，不同算法限制不同
TEST_F(AllGatherTest, allgather_executor_test_loop)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;

    for (int i = 0; i < allGatherAlgName.size(); i++) {
        testOpParam.algName = allGatherAlgName[i];
        Checker checker;
        HcclResult ret;
        ret = checker.Check(testOpParam, ranktable, topoMeta);
        if (ret == HcclResult::HCCL_SUCCESS) {
            std::cout << allGatherAlgName[i] + " run successed" << std::endl;
        } else {
            std::cout << allGatherAlgName[i] + " run failed" << std::endl;
        }
        EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
    }
}

TEST_F(AllGatherTest, allgather_AllGatherComm_test)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta {{{0, 1}, {0, 1, 2}}};
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;

    testOpParam.algName = "AllGatherComm";
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);

    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_AllGatherSingleExecutor_test)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 1);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGatherSingleExecutor";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;

    testOpParam.algName = "AllGatherSingleExecutor";
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);

    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910A_offload_AllGatherMeshExecutor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 4);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherMeshExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910A_offload_AllGatherRingExecutor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 2, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherRingExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910A_offload_AllGatherComm)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta {{{0, 1}, {0, 1, 2}}};
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherComm";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910A_opbase_AllGatherMeshExecutor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 4);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherMeshExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910A_opbase_AllGatherRingExecutor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 2, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherRingExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910A_opbase_AllGatherComm)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta {{{0, 1}, {0, 1, 2}}};
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherComm";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910_93_opbase_AlignedAllGatherDoubleRingFor91093Executor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AlignedAllGatherDoubleRingFor91093Executor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910_93_offload_AlignedAllGatherDoubleRingFor91093Executor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AlignedAllGatherDoubleRingFor91093Executor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910_93_opbase_multiSuperPod_2_6_2_AllGatherRingFor91093Executor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 2, 6, 2);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910_93_opbase_multiSuperPod_4_3_2_AllGatherRingFor91093Executor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 4, 3, 2);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910_93_opbase_AllGatherRingFor91093Executor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherRingFor91093Executor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}
// 存在stream越界问题，需要对应的算法进行定位
#if 0
TEST_F(AllGatherTest, allgather_910_93_offload_AllGatherRingFor91093Executor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherRingFor91093Executor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}
#endif

TEST_F(AllGatherTest, allgather_910_93_offload_AllGatherDoubleRingConcurrentExecutor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    setenv("HCCL_CONCURRENT_ENABLE", "1", 1);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherDoubleRingConcurrentExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_310P3_opbase_AllGatherFor310PExecutor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_310P3;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherFor310PExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_310P3_offload_AllGatherFor310PExecutor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_310P3;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherFor310PExecutor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_smallcount_multiserver_test)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 2, 12);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;

    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);

    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910_93_offload_AllGatherSemiRingExecutor_singleServer)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 4);
    gen.GenRankTable(topoMeta, ranktable);
 
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT32;
    testOpParam.algName = "AllGatherSemiRingExecutor";
    testOpParam.aicpuUnfoldMode = true;
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_910_93_offload_AllGatherSemiRingExecutor_singleServer_oddNum)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 4);
    gen.GenRankTable(topoMeta, ranktable);
 
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.DataDes.count = 11;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.algName = "AllGatherSemiRingExecutor";
    testOpParam.aicpuUnfoldMode = true;
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

#if 0 // 待解决资源申请后使用
TEST_F(AllGatherTest, allgather_910_93_offload_AllGatherSemiRingExecutor_MultiServer)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 4, 6);
    gen.GenRankTable(topoMeta, ranktable);
    setenv("HCCL_ALGO", "level0:ring;level1:NHR", 1);
 
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.algName = "AllGatherSemiRingExecutor";
    testOpParam.aicpuUnfoldMode = true;
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}
 
TEST_F(AllGatherTest, allgather_910_93_offload_AllGatherSemiRingExecutor_MultiSuPod)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 2, 4, 8);
    gen.GenRankTable(topoMeta, ranktable);
    setenv("HCCL_ALGO", "level0:ring;level1:NHR", 1);
 
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.algName = "AllGatherSemiRingExecutor";
    testOpParam.aicpuUnfoldMode = true;
 
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}
#endif

TEST_F(AllGatherTest, allgather_ax_4server_16p)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 4, 16);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.devtype = DevType::DEV_TYPE_910B;

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_mix_AllGatherMixExecutor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;

    TopoMeta topoMeta {{{0, 1, 2}, {0, 1, 2, 3, 4, 5, 6, 7, 8}}};
    gen.GenRankTable(topoMeta, ranktable);
    for (auto &rankInfo : ranktable.rankList) {
        if (rankInfo.serverIdx == 0) {
            rankInfo.deviceInfo.deviceType = DevType::DEV_TYPE_910B;
        } else {
            rankInfo.deviceInfo.deviceType = DevType::DEV_TYPE_910_93;
        }
    }

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.devTypes.insert(DevType::DEV_TYPE_910B);
    testOpParam.devTypes.insert(DevType::DEV_TYPE_910_93);

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_mix_AllGatherMixExecutorComm)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;

    TopoMeta topoMeta {{{0}, {0}}};
    gen.GenRankTable(topoMeta, ranktable);
    for (auto &rankInfo : ranktable.rankList) {
        if (rankInfo.serverIdx == 0) {
            rankInfo.deviceInfo.deviceType = DevType::DEV_TYPE_910B;
        } else {
            rankInfo.deviceInfo.deviceType = DevType::DEV_TYPE_910_93;
        }
    }

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.devTypes.insert(DevType::DEV_TYPE_910B);
    testOpParam.devTypes.insert(DevType::DEV_TYPE_910_93);

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_aiv_a3_AllGatherVMeshAivExecutor)
{
    MOCKER(GetExternalInputHcclAivMode).stubs().will(returnValue(true));

    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 16);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.aicpuUnfoldMode = true;

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_AllGatherSmallCountExecutor)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 4, 4);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.algName = "AllGatherSmallCount";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_AllGatherRingFor91093Executor_NHR)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 2, 2, 8);
    gen.GenRankTable(topoMeta, ranktable);

    setenv("HCCL_ALGO", "level0:NA;level1:NHR;level2:NHR", 1);
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.algName = "AllGatherRingFor91093Executor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_AllGatherRingFor91093Executor_NB)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 2, 2, 8);
    gen.GenRankTable(topoMeta, ranktable);

    setenv("HCCL_ALGO", "level0:NA;level1:NB;level2:NB", 1);
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OPBASE;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;
    testOpParam.algName = "AllGatherRingFor91093Executor";

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(AllGatherTest, allgather_A3_2Server1Rank_nb_test)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 2, 1);
    gen.GenRankTable(topoMeta, ranktable);

    setenv("HCCL_ALGO", "level0:NA;level1:NB", 1);
    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_ALLGATHER;
    testOpParam.tag = "AllGather";
    testOpParam.opMode = OFFLOAD;
    testOpParam.DataDes.count = 100;
    testOpParam.DataDes.dataType = HcclDataType::HCCL_DATA_TYPE_INT8;
    testOpParam.devtype = DevType::DEV_TYPE_910_93;

    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);
    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}